package com.smyx.demo.queue;

/**
 * @author SmyxMac
 * @version 1.0.0
 * @Description 数组实现队列(非环形)
 * @createTime 2023年05月09日 22:14:00
 */
public class ArrayNormalQueue {
    // 队列最大长度
    private final int maxSize;
    // 队列头index
    private int head;
    // 队列尾index
    private int tail;
    private final int[] arrQueue;

    public ArrayNormalQueue(int maxSize) {
        this.maxSize = maxSize;
        this.head = -1;
        this.tail = -1;
        this.arrQueue = new int[maxSize];
    }

    /**
     * 向队列中添加数据
     * @param n 待添加的数据
     * @author SmyxMac
     * @date: 2023/5/9 22:23
     */
    public void addData(int n) {
        if (isFull()) {
            throw new RuntimeException("队列已经满了,无法存储数据!!!");
        }
        tail++;
        arrQueue[tail] = n;
    }

    /***
     * 获取队列数据
     * @author SmyxMac
     * @date: 2023/5/9 22:27
     */
    public int getData() {
        if (isEmpty()) {
            throw new RuntimeException("队列是空的,无法获取数据!!!");
        }
        head++;
        return arrQueue[head];
    }

    /**
     * 获取队列头数据
     * @author SmyxMac
     * @date: 2023/5/9 22:50
     * @return: int 队列头数据
     */
    public int getHeadData() {
        if (isEmpty()) {
            throw new RuntimeException("队列是空的,无法获取数据!!!");
        }
        return arrQueue[head + 1];
    }

    /**
     * 判断队列是否为空
     * @author SmyxMac
     * @date: 2023/5/9 22:19
     * @return: boolean 是否为空
     */
    public boolean isEmpty() {
        return head == tail;
    }

    /**
     * 判断队列是否已经满了
     * @author SmyxMac
     * @date: 2023/5/9 22:20
     * @return: boolean 是否已满
     */
    public boolean isFull() {
        return tail == maxSize - 1;
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        for (int i = 0; i < arrQueue.length; i++) {
            System.out.printf("arr[%d]=%d \t", i, arrQueue[i]);
        }
        System.out.println();
        System.out.println("============================================================================================================================================");
    }

}
